package com.tyros.storage.enums;

import com.tyros.web.support.message.BaseException;
import com.tyros.storage.constant.ResourceMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * @author zhangshuaiyin
 * @date 2022/2/20 11:41
 */
@Getter
@AllArgsConstructor
public enum StorageType {

    /**
     * 存储类型
     */
    LOCAL(1, StorageType.LOCAL_TYPE),
    FAST_DFS(2, StorageType.FAST_DFS_TYPE),
    ALI_YUN(3, StorageType.ALI_YUN_OSS),
    ;

    public static final String LOCAL_TYPE = "LOCAL";
    public static final String FAST_DFS_TYPE = "FAST_DFS";
    public static final String ALI_YUN_OSS = "ALI_YUN";

    private final Integer code;
    private final String type;

    public boolean equals(String type) {
        return Objects.equals(this.type, type);
    }

    public boolean equals(Integer code) {
        return Objects.equals(this.code, code);
    }

    public static StorageType get(Integer code) {
        for (StorageType storageType : values()) {
            if (storageType.equals(code)) {
                return storageType;
            }
        }
        throw new BaseException(ResourceMessage.STORAGE_TYPE_NOT_EXIST);
    }

    public static StorageType get(String type) {
        for (StorageType storageType : values()) {
            if (storageType.equals(type)) {
                return storageType;
            }
        }
        throw new BaseException(ResourceMessage.STORAGE_TYPE_NOT_EXIST);
    }
}
